package com.tlvip;

/**
 * <p>description：</p>
 * <p>copyright： copyright(C)2016-2099</p>
 * <p>Life is short,we need passion</p>
 * <p>Summary： 本程序演示缓存行在命中与不命中情况下，执行相同加法求和耗时差异
 * 线程上下文切换时，线程信息保存在TSS中
 *
 * 虚拟机指令集架构：
 *  1.栈指令集架构；
 *  2.寄存器指令集架构；
 *
 * 线程模型：
 *  1.ULT
 *  2.KLT ——> jvm使用此种模型
 * </p>
 * <p>instructions： </p>
 * Date 2022/5/21</p>
 * Author mac
 *
 * @version 1.0
 */
public class TwoDimentionalArraySum {
    private static final int RUMS = 100;

    private final static int DIMENSION_1 = 1024 * 1024;

    private final static int DIMENSION_2 = 6;

    private static long[][] longs;

    public static void main(String[] args) {
        longs = new long[DIMENSION_1][DIMENSION_2];
        for (int i = 0; i < DIMENSION_1; i++) {
            for (int j = 0; j < DIMENSION_2; j++) {
                longs[i][j] = 1L;
            }
        }
        System.out.println("init longs..");
        long sum = 0L;
        long start = System.currentTimeMillis();
        for (int i = 0; i < DIMENSION_1; i++) {
            for (int j = 0; j < DIMENSION_2; j++) {
                sum += longs[i][j];
            }
        }
        long end = System.currentTimeMillis();
        System.out.println("sum1:" + sum);
        System.out.println("1 cost:" + (end - start));

        sum = 0L;
        start = System.currentTimeMillis();
        for (int j = 0; j < DIMENSION_2; j++) {
            for (int i = 0; i < DIMENSION_1; i++) {
                sum += longs[i][j];
            }
        }
        end = System.currentTimeMillis();
        System.out.println("sum2:" + sum);
        System.out.println("2 cost:" + (end - start));
    }
}
